package com.example.fruit.ui.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.fruit.R;
import com.example.fruit.bean.Browse;
import com.example.fruit.bean.Car;
import com.example.fruit.bean.Collect;
import com.example.fruit.bean.Fruit;
import com.example.fruit.util.SPUtils;
import com.example.fruit.widget.ActionBar;

import org.litepal.crud.DataSupport;

import java.text.SimpleDateFormat;

/**
 * 水果详情信息
 */
public class FruitDetailActivity extends AppCompatActivity {
    private Activity mActivity;
    private ImageView ivImg;
    private TextView tvTitle;
    private TextView tvDate;
    private TextView tvContent;
    private TextView tvIssuer;
    private TextView tvAmount;
    private Button btnCollect;
    private Button btnCancel,buy;
    private ImageView collect,collect1;
    private ActionBar mActionBar;//标题栏
    private SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = this;
        setContentView(R.layout.activity_fruit_detail);
        ivImg = findViewById(R.id.img);
        tvTitle= findViewById(R.id.title);
        tvDate = findViewById(R.id.date);
        tvContent = findViewById(R.id.content);
        tvIssuer = findViewById(R.id.issuer);
        tvAmount = findViewById(R.id.amount);
        btnCollect = findViewById(R.id.btn_collect);
        btnCancel = findViewById(R.id.btn_cancel);
        buy = findViewById(R.id.btn_buy);
        collect = findViewById(R.id.collect);
        collect1 = findViewById(R.id.collect1);
        mActionBar = findViewById(R.id.myActionBar);
        //侧滑菜单
        mActionBar.setData(mActivity,"商品详情", R.drawable.ic_back, 0, 0, getResources().getColor(R.color.colorPrimary), new ActionBar.ActionBarClickListener() {
            @Override
            public void onLeftClick() {
                finish();
            }

            @Override
            public void onRightClick() {
            }
        });
        Fruit fruit = (Fruit) getIntent().getSerializableExtra("fruit");
        tvTitle.setText(fruit.getTitle());
        tvDate.setText(String.format("上架时间       %s",fruit.getDate()));
        tvContent.setText(String.format("商品介绍：      "+"\n\n"+"%s",fruit.getContent()));
        tvIssuer.setText(String.format("￥ %s",fruit.getIssuer()));
        tvAmount.setText(String.format("库存：%s",fruit.getAmount()));
        Glide.with(mActivity)
                .asBitmap()
                .skipMemoryCache(true)
                .load(fruit.getImg())
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(ivImg);
        String account = (String) SPUtils.get(mActivity,SPUtils.ACCOUNT,"");
        Browse browse = DataSupport.where("account = ? and title = ?",account,fruit.getTitle()).findFirst(Browse.class);//浏览记录
        if (browse == null) {//不存在该条浏览记录  新增记录
            Browse browse1 = new Browse(account,fruit.getTitle());
            browse1.save();
        }
        Boolean isAdmin = (Boolean) SPUtils.get(mActivity,SPUtils.IS_ADMIN,false);
        if (!isAdmin){
            Car car = DataSupport.where("account = ? and title = ?",account,fruit.getTitle()).findFirst(Car.class);
            btnCollect.setVisibility(car!=null?View.GONE:View.VISIBLE);
            btnCancel.setVisibility(car!=null?View.VISIBLE:View.GONE);

            Collect collects = DataSupport.where("account = ? and title = ?",account,fruit.getTitle()).findFirst(Collect.class);
            collect.setVisibility(collects!=null?View.GONE:View.VISIBLE);
            collect1.setVisibility(collects!=null?View.VISIBLE:View.GONE);

        }

        //加入购物车
        btnCollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Car car = new Car(fruit.getIssuer(),account,fruit.getTitle());
                car.save();
                Toast.makeText(mActivity,"加入购物车成功", Toast.LENGTH_SHORT).show();
                btnCollect.setVisibility(View.GONE);
                btnCancel.setVisibility(View.VISIBLE);
            }
        });
        //收藏
        collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Collect collects = new Collect(account,fruit.getTitle());
                collects.save();
                Toast.makeText(mActivity,"收藏成功", Toast.LENGTH_SHORT).show();
                collect.setVisibility(View.GONE);
                collect1.setVisibility(View.VISIBLE);
            }
        });
        //取消
        collect1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Collect collects = DataSupport.where("account = ? and title = ?",account,fruit.getTitle()).findFirst(Collect.class);
                collects.delete();
                Toast.makeText(mActivity,"取消收藏", Toast.LENGTH_SHORT).show();
                collect.setVisibility(View.VISIBLE);
                collect1.setVisibility(View.GONE);
            }
        });
        //立即购买
        buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转页面
                Car car = new Car(fruit.getIssuer(),account,fruit.getTitle());
                Intent intent = new Intent(mActivity, BuyActivity.class);
                Fruit fruit = DataSupport.where("title = ?",car.getTitle()).findFirst(Fruit.class);
                intent.putExtra("fruit",fruit);
                mActivity.startActivity(intent);
                return;
             }
        });


    }

    //取消购物车
    public void cancel(View view){
        Fruit fruit = (Fruit) getIntent().getSerializableExtra("fruit");
        String account = (String) SPUtils.get(mActivity,SPUtils.ACCOUNT,"");
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.a1);//设置图标
        builder.setTitle("水果商城");//设置对话框的标题
        builder.setMessage("确定要将该水果从购物车移除吗？");//设置对话框内容
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Car order = DataSupport.where("account = ? and title = ?",account,fruit.getTitle()).findFirst(Car.class);
                order.delete();
                Toast.makeText(mActivity,"已从购物车移除", Toast.LENGTH_SHORT).show();
                btnCollect.setVisibility(View.VISIBLE);
                btnCancel.setVisibility(View.GONE);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        }
        );
        AlertDialog a=builder.create();
        a.show();  //必须show一下才能看到对话框
    }
    public void QQ(View view){
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.a1);//设置图标
        builder.setTitle("水果商城");//设置对话框的标题
        builder.setMessage("确定要联系客服吗？");//设置对话框的内容
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //跳转到我的资料卡
                ProgressDialog dialog2 = new ProgressDialog(mActivity);
                dialog2.setMessage("正在连接客服···");
                dialog2.show();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Toast.makeText(mActivity,"如无法联系，请添加好友后再试!", Toast.LENGTH_LONG).show();
                            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("mqqapi://card/show_pslcard?src_type=internal&source=sharecard&version=1&uin=1632957243")));//跳转到QQ资料
                        }catch (Exception e){
                            Toast.makeText(mActivity,"请安装最新版QQ后使用此功能!", Toast.LENGTH_LONG).show();
                        }
                        dialog2.dismiss();
                    }
                }, 1500);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                }
        );
        AlertDialog a=builder.create();
        a.show();  //必须show一下才能看到对话框
    }
}
